<template>
    <div>
        <div class="background">
            <el-carousel indicator-position="outside"  :interval="4000" height="800px">
                <el-carousel-item v-for="(p,i) in loginImg" :key="i">
                    <img :src="p.url" width="100%" height="100%">
                </el-carousel-item>
            </el-carousel>
            <div class="contain">
                <h1>设置新密码</h1>
                <div class="form">
                    <label>手机号：</label><input type="text" v-model.trim="tel" placeholder="请输入手机号"><br/>
                    <div v-if="myshow4" class="tishi">手机号格式错误！</div>
                </div>
                
                <div class="form">
                    <label>验证码：</label><input type="text" v-model.trim="yzm" placeholder="请输入验证码"><br/>
                    <!-- <div @click="yzmClick" class="yzm">{{timeTitle}}</div> -->
                    <!-- 引入验证码组件 -->
                    <div @click="changeCode()" class="yzm">
                      <identify :identifyCode="identifyCode"></identify>
                    </div>
                    <div v-if="myshow5" class="tishi">验证码输入错误！</div>
                    
                </div>
                <div class="form">
                    <label>密码：</label><input type="password" v-model.trim="password" placeholder="请输入新密码"><br/>
                    <div v-if="myshow1" class="tishi">密码格式错误！</div>
                </div>
                <div class="form">
                    <label>确认密码：</label><input type="password" v-model.trim="repassword" placeholder="再次确认密码"><br/>
                    <div v-if="myshow2" class="tishi">两次输入的密码不一致！</div>
                </div>
                
                
                <button @click.prevent="handlefinish">提交</button>
            </div>
        </div>
        <my_footer/>
    </div>
    
</template>

<script>
// 引入验证码组件
import identify from '@/components/Identify.vue'
export default {
    name:'resetpwd',
    // 注册验证码组件
    components: { identify },
    props: {
    msg: String
  },
  data(){
    return{
       // 验证码初始值
      identifyCode: '1234',
      // 验证码的随机取值范围
      identifyCodes: '1234567890',
      myshow1:false,
      myshow2:false,
			myshow4:false,
      tel:"",
      password:"",
      repassword:"",
      yzm:"",
      timeTitle: '获取验证码',
      loginImg:[
             {id:1,url:require("../../public/home/banner1.jpg")},
             {id:2,url:require("../../public/home/banner2.jpg")},
             {id:3,url:require("../../public/home/banner3.jpg")},
             {id:4,url:require("../../public/home/banner4.jpg")}
         ]
    };
  },
  mounted () {
    // 刷新页面就生成随机验证码
    this.identifyCode = ''
    this.makeCode(this.identifyCodes, 4)
  },
  methods:{
    // 点击验证码刷新验证码
    changeCode () {
      this.identifyCode = ''
      this.makeCode(this.identifyCodes, 4)
    },
    // 生成一个随机整数  randomNum(0, 10) 0 到 10 的随机整数， 包含 0 和 10
    randomNum (min, max) {
      max = max + 1
      return Math.floor(Math.random() * (max - min) + min)
    },
    // 随机生成验证码字符串
    makeCode (data, len) {
      for (let i = 0; i < len; i++) {
        this.identifyCode += data[this.randomNum(0, data.length - 1)]
      }
    },
      // 点击获取验证码
			yzmClick() {
				this.timer(60)
			},
			// 倒计时
			timer(Second) {
				let timeSecond = Second // 倒计时时间秒
				let time = setInterval(res => {
					this.timeTitle = timeSecond--
					if (timeSecond == 0) {
						this.timeTitle = '重新获取验证码',
							clearTimeout(time) // 清除Interval的定时器
					}
				}, 1000)
			},
  //点击完成按钮触发handlefinish
    handlefinish:function()
    {
      if(localStorage['tel']!==this.tel)
      {
        
        this.$message("该用户不存在");
      }
      else if(this.tel==='')
      {
        this.$message("手机号不能为空");
      }else if(this.yzm===''){
          this.$message("验证码不能为空");
      }else if(this.password===''){
          this.$message("密码不能为空");
      }else if(this.repassword===''){
          this.$message("密码不能为空");
      }
      else{//将新的密码信息存储到localStorage
        localStorage.setItem('password',this.password);
        this.$message
        ("修改新密码成功");
        this.$router.replace('/Login');//完成后跳转至登录页面
      }
    }
  },
  watch:{
    tel(){
			let reg4 = /^[1][3,4,5,7,8][0-9]{9}$/
			if(!reg4.test(this.tel)){
				this.myshow4=true
			}else if(reg4.test(this.tel)){
				this.myshow4=false
			}
			else{
				this.myshow4=false
			}
      if(this.tel==""){
        this.myshow4=false
      }
		},
    password(){
			//强密码(必须包含大小写字母和数字的组合，可以使用特殊字符，长度在 8-10 之间)
			let reg1 = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$/;
			if(!reg1.test(this.password)){
				this.myshow1=true
			}else if(reg1.test(this.password)){
				this.myshow1=false
			}
			else{
				this.myshow1=false
				
			}
      if(this.password==""){
        this.myshow1=false
      }
		},
    repassword(){
      if(this.repassword!==this.password){
        this.myshow2=true
      }else{
        this.myshow2=false
      }
      if(this.repassword==""){
        this.myshow2=false
      }
    },
    yzm(){
      if(this.yzm!==this.identifyCode){
        this.myshow5=true
      }else{
        this.myshow5=false
      }
      if(this.yzm==""){
        this.myshow5=false
      }
    }
  }
};
</script>

//css
<style scoped lang="scss">
.tishi {
    font-family: PingFangSC-Regular;
    font-size: 13px;
    font-weight: normal;
    font-stretch: normal;
    letter-spacing: 0;
    color: red;
    position: absolute;
    left: 260px;
  }
.yzm {
					font-family: PingFangSC-Regular;
					font-size: 14px;
					font-weight: normal;
					font-stretch: normal;
					letter-spacing: 0;
					color: #4d77ff;
          position: absolute;
          right: 13px;
          top: 242px;
				}
  .el-carousel__item img {
            height:994px;
            width:100%;
        }
        .el-carousel__item:nth-child(2n) {
            background-color: #99a9bf;
        }
        .el-carousel__item:nth-child(2n+1) {
            background-color: #d3dce6;
        }
.background{
    width:100%;
    /* height: 880px; */
    /* background: url("../../public/home/banner1.jpg"); */
    background-size:100% 100%;
    position: relative;
    top: 0;
    left: 0; 
}
.contain{
z-index: 100;
  width: 650px;
  height: 600px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  background:#00000090;
  text-align: center;
  border-radius: 20px;
  
}
.contain h1{
  color: white;
  margin-top: 50px;
}
.form{
  color: white;
  margin-left: 20%;
  margin-top: 60px;
  font-size: 20px;
  text-align: left;
}
label{
  float:left;
  width: 5em;
  margin-right: 1em;
  text-align: right;
}
input,textarea{
  margin-left: 10px;
  padding: 15px;
  border: solid 1px #ebd404;
  outline: 0;
  font: normal 13px/100% Verdana,Tahoma,sans-serif;
  width: 250px;
  height: 30px;
  background:#f1f1f190;
  box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 8px;
  border-radius: 10px;
}
input:hover,textarea:hover,input:focus,textarea:focus{border-color:#0d0aa1;}
button{
  position: relative;
  height: 33px;
  width: 150px;
  background: rgba(115, 108, 216, 0.425);
  border-radius: 10px;
  margin-top: 80px;
  box-shadow: none;
  color: white;
  margin-left: 40px;
}
</style>
